package ${basePackage};

import com.orientsec.dts.option.enums.MessageType;
import com.orientsec.dts.option.enums.VersionNumber;
import com.orientsec.dts.option.exceptions.OptionIllegalException;
import com.orientsec.dts.option.messages.query.DealsQuery;
import com.orientsec.dts.option.xbdep.codec.MessageDeserializer;
import com.orientsec.dts.option.xbdep.codec.MessageSerializer;
import com.orientsec.dts.option.xbdep.comm.BinaryUtils;
import com.orientsec.dts.option.xbdep.comm.FutureMessageConverter;
import com.orientsec.dts.option.xbdep.comm.Message;

/**
 * @description: ${msgDesc}
 * @author: ${author}
 */
public class ${msgName} extends Message implements FutureMessageConverter<${outMsgName}> {


    private static int MSG_TYPE;

    public ${msgName}(int versionNumber) {
<#if version_list?exists>
   <#list version_list as version>
        <#if version_index == 0>
        if (versionNumber == ${version}.getCode()) {
            setMsgType(${version}.getCode());
        <#else >
        } else if (versionNumber == ${version}.getCode()) {
            setMsgType(${version}.getCode());
        </#if>
   </#list>
        } else {
            throw new OptionIllegalException("API版本号错误!");
        }
        MSG_TYPE = getMsgType();
   }
</#if>

<#if param_combine_list?exists>
    <#list param_combine_list as param>
        <#if (param.msgParamType?lower_case = 'innerclass')>
    /**
     * ${param.paramDesc}
     * 数据类型 ${param.charDesc}
     */
    private List<${param.outParamName}> ${param.paramName} = new ArrayList<>();
        <#else>
    /**
     * ${param.paramDesc}
     * 数据类型 ${param.charDesc}
     */
    private ${param.msgParamType} ${param.paramName};
        </#if>
    </#list>
</#if>

    @Override
    public ${outMsgName} toFutureMessage() {
        return null;
    }

    @Override
    public void fromFutureMessage(${outMsgName} fm) {
    <#if param_combine_list?exists>
        <#list param_combine_list as param>
            <#if (param.baseType?lower_case = 'base')>
        ${param.paramName} = fm.get${param.outParamName?cap_first}();
            <#elseif (param.baseType?lower_case = 'innerclass')>
        for (${outMsgName}.${param.outParamName} out${param.paramName?cap_first} : fm.get${param.outParamName?cap_first}()) {
            ${msgName}.${param.outParamName} inner${param.paramName?cap_first} = new ${msgName}.${param.outParamName}();
                <#assign inner_param_list = inner_class['param_combine_list']>
                <#list inner_param_list as inner_param>
                    <#if (inner_param.baseType?lower_case = 'base')>
            inner${param.paramName?cap_first}.${inner_param.paramName} = out${param.paramName?cap_first}.get${inner_param.paramName?cap_first}();
                    <#else >
            inner${param.paramName?cap_first}.${inner_param.paramName} = out${param.paramName?cap_first}.get${inner_param.paramName?cap_first}().getCode();
                    </#if>
                </#list>
            ${param.paramName}.add(inner${param.paramName?cap_first});
        }
            <#else >
        ${param.paramName} = fm.get${param.outParamName?cap_first}().getCode();
            </#if>
        </#list>
    </#if>
    }

    @Override
    public int getBodyLength() {
        int bodyLength = 0;
    <#if param_list?exists>
        <#list param_list as param>
            <#if (param.version?lower_case = 'all')>
                <#if (param.paramCharType?lower_case = 'char')>
                    <#if param.charLength?lower_case = 'change'>
        bodyLength += BinaryUtils.SIZE_INT32 + BinaryUtils.SIZE_CHAR * ${param.paramName}.length(); //${param.paramName}
                    <#else>
        bodyLength += BinaryUtils.SIZE_CHAR * ${param.charLength}; //${param.paramName}
                    </#if>
                <#elseif ((param.paramCharType?lower_case = 'int64') || (param.paramCharType?lower_case = 'amt') ||(param.paramCharType?lower_case = 'qty'))>
        bodyLength += BinaryUtils.SIZE_INT64; //${param.paramName}
                <#elseif (param.paramCharType?lower_case = 'int16')>
        bodyLength += BinaryUtils.SIZE_INT16; //${param.paramName}
                <#elseif (param.paramCharType?lower_case = 'uint16')>
        bodyLength += BinaryUtils.SIZE_UINT16; //${param.paramName}
                <#elseif (param.paramCharType?lower_case = 'uint8')>
        bodyLength += BinaryUtils.SIZE_UINT8; //${param.paramName}
                <#elseif (param.paramCharType?lower_case = 'innerclass')>
                    <#if inner_class?exists>
                        <#assign inner_param_list = inner_class['param_combine_list']>
                        <#list inner_param_list as inner_param>
                            <#if (inner_param.version?lower_case = 'all')>
                                <#if (inner_param.paramCharType?lower_case = 'char')>
                                    <#if inner_param.charLength?lower_case = 'change'>
        bodyLength += ${param.paramName}.size() * (BinaryUtils.SIZE_INT32 + BinaryUtils.SIZE_CHAR * ${inner_param.paramName}.length()); //${inner_param.paramName}
                                    <#else>
        bodyLength += ${param.paramName}.size() * (BinaryUtils.SIZE_CHAR * ${inner_param.charLength}); //${inner_param.paramName}
                                    </#if>
                                <#elseif ((inner_param.paramCharType?lower_case = 'int64') || (inner_param.paramCharType?lower_case = 'amt') || (inner_param.paramCharType?lower_case = 'qty'))>
        bodyLength += ${param.paramName}.size() * (BinaryUtils.SIZE_INT64); //${inner_param.paramName}
                                <#elseif (inner_param.paramCharType?lower_case = 'int16')>
        bodyLength += ${param.paramName}.size() * (BinaryUtils.SIZE_INT16); //${inner_param.paramName}
                                <#elseif (param.paramCharType?lower_case = 'uint16')>
        bodyLength += ${param.paramName}.size() * (BinaryUtils.SIZE_UINT16); //${inner_param.paramName}
                                <#elseif (inner_param.paramCharType?lower_case = 'uint8')>
        bodyLength += ${param.paramName}.size() * (BinaryUtils.SIZE_UINT8); //${inner_param.paramName}
                                </#if>
                            <#else >
                                <#list param.versionList as version>
                        if (getMsgType() == MessageType.${version}.getCode()) {
                                    <#if (param.paramCharType?lower_case = 'char')>
                                        <#if param.charLength?lower_case = 'change'>
        bodyLength += ${param.paramName}.size() * (BinaryUtils.SIZE_INT32 + BinaryUtils.SIZE_CHAR * ${param.paramName}.length()); //${param.paramName}
                                        <#else>
        bodyLength += ${param.paramName}.size() * (BinaryUtils.SIZE_CHAR * ${param.charLength}); //${param.paramName}
                                        </#if>
                                    <#elseif ((param.paramCharType?lower_case = 'int64') || (param.paramCharType?lower_case = 'amt') || (param.paramCharType?lower_case = 'qty'))>
        bodyLength += ${param.paramName}.size() * (BinaryUtils.SIZE_INT64); //${param.paramName}
                                    <#elseif (param.paramCharType?lower_case = 'int16')>
        bodyLength += ${param.paramName}.size() * (BinaryUtils.SIZE_INT16); //${param.paramName}
                                    <#elseif (param.paramCharType?lower_case = 'uint16')>
        bodyLength += ${param.paramName}.size() * (BinaryUtils.SIZE_UINT16); //${param.paramName}
                                    <#elseif (param.paramCharType?lower_case = 'uint8')>
        bodyLength += ${param.paramName}.size() * (BinaryUtils.SIZE_UINT8); //${param.paramName}
                                    </#if>
                        }
                                </#list>
                            </#if>
                        </#list>

                    </#if>
                </#if>
            <#else >
              <#list param.verList as ver>
        if (getMsgType() == MessageType.${ver}.getCode()) {
                <#if (param.paramCharType?lower_case = 'char')>
                    <#if param.charLength?lower_case = 'change'>
            bodyLength += BinaryUtils.SIZE_INT32 + BinaryUtils.SIZE_CHAR * ${param.paramName}.length(); //${param.paramName}
                    <#else>
            bodyLength += BinaryUtils.SIZE_CHAR * ${param.charLength}; //${param.paramName}
                    </#if>
                <#elseif ((param.paramCharType?lower_case = 'int64') || (param.paramCharType?lower_case = 'amt') || (param.paramCharType?lower_case = 'qty'))>
            bodyLength += BinaryUtils.SIZE_INT64; //${param.paramName}
                <#elseif (param.paramCharType?lower_case = 'int16')>
            bodyLength += BinaryUtils.SIZE_INT16; //${param.paramName}
                <#elseif (param.paramCharType?lower_case = 'uint16')>
            bodyLength += BinaryUtils.SIZE_UINT16; //${param.paramName}
                <#elseif (param.paramCharType?lower_case = 'uint8')>
            bodyLength += BinaryUtils.SIZE_UINT8; //${param.paramName}
                </#if>
        }
              </#list>
            </#if>
        </#list>
    </#if>
        return bodyLength;
    }

    @Override
    public byte[] encodeMessage(MessageSerializer serializer) {
        return serializer.serialize(this);
    }

    @Override
    public void decode(MessageDeserializer deserializer, byte[] buf) {

    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("${msgName} [");
        <#if param_combine_list?exists>
            <#list param_combine_list as param>
        sb.append("${param.paramName} =").append(${param.paramName});
            </#list>
        </#if>
        sb.append("MsgType=").append(getMsgType());
        sb.append("]");
        return sb.toString();
    }

    <#if inner_class?exists>
    /**
     * @description: ${inner_class['msgDesc']}
     */
    public static class ${inner_class['msgName']?cap_first} {
            <#if inner_class['param_combine_list']?exists>
                <#list inner_class['param_combine_list'] as param>
        /**
         * ${param.paramDesc}
         * 数据类型 ${param.charDesc}
         */
        private ${param.msgParamType} ${param.paramName};
                </#list>
            </#if>

        @Override
        public String toString() {
            StringBuffer sb = new StringBuffer();
            sb.append("${inner_class['msgName']} [");
            <#if inner_class['param_combine_list']?exists>
                <#list inner_class['param_combine_list'] as param>
            sb.append("${param.paramName} =").append(${param.paramName});
                </#list>
            </#if>
            sb.append("]");
            return sb.toString();
        }

        <#if inner_class['param_combine_list']?exists>
            <#list inner_class['param_combine_list'] as param>
        public void set${param.paramName?cap_first}(${param.msgParamType} ${param.paramName}) {
            this.${param.paramName} = ${param.paramName};
        }

        public ${param.msgParamType} get${param.paramName?cap_first}() {
            return ${param.paramName};
        }

            </#list>
        </#if>
    }
    </#if>

    <#if param_combine_list?exists>
        <#list param_combine_list as param>
            <#if (param.msgParamType?lower_case = 'innerclass')>
    public void set${param.outParamName}(List<${param.outParamName}> ${param.paramName}) {
        this.${param.paramName} = ${param.paramName};
    }

    public List<${param.outParamName}> get${param.paramName?cap_first}() {
        return ${param.paramName};
    }

            <#else >
    public void set${param.paramName?cap_first}(${param.msgParamType} ${param.paramName}) {
        this.${param.paramName} = ${param.paramName};
    }

    public ${param.msgParamType} get${param.paramName?cap_first}() {
        return ${param.paramName};
    }

            </#if>
        </#list>
    </#if>
}
